package com.cskaoyan.service.configmanagement;

import com.cskaoyan.bean.configmanagement.Express;
import com.cskaoyan.bean.configmanagement.MallVO;
import com.cskaoyan.bean.configmanagement.Order;
import com.cskaoyan.bean.configmanagement.WX;
import com.cskaoyan.mapper.configmanagement.ConfigManagementMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;

@Service
public class ConfigManagementServiceImpl implements ConfigManagementService {
    @Autowired
    ConfigManagementMapper configManagementMapper;
    @Override
    public MallVO mallEcho() throws IllegalAccessException {
        MallVO mallVO = new MallVO();
        MallVO mallVO1 = selectValue(mallVO);
        return mallVO1;
    }

    @Override
    public void mallModify(MallVO mallVO) throws IllegalAccessException {
        setValue(mallVO);
    }

    @Override
    public Express expressEcho() throws IllegalAccessException {
        return selectValue(new Express());
    }

    @Override
    public void expressModify(Express express) throws IllegalAccessException {
        setValue(express);
    }

    @Override
    public Order orderEcho() throws IllegalAccessException {
        return selectValue(new Order());
    }

    @Override
    public void orderModify(Order order) throws IllegalAccessException {
        setValue(order);
    }

    @Override
    public WX WXEcho() throws IllegalAccessException {
        return selectValue(new WX());
    }

    @Override
    public void WXModify(WX wx) throws IllegalAccessException {
        setValue(wx);
    }


    private <T> T selectValue(T t) throws IllegalAccessException {
        Field[] declaredFields = t.getClass().getDeclaredFields();
        for (Field declaredField : declaredFields) {
            declaredField.setAccessible(true);
            String name = declaredField.getName();
            String value = configManagementMapper.selectByName(name);
            declaredField.set(t,value);
        }
        return t;
    }
    private <T> void setValue(T t) throws IllegalAccessException {
        Field[] declaredFields = t.getClass().getDeclaredFields();
        for (Field declaredField : declaredFields) {
            declaredField.setAccessible(true);
            String name = declaredField.getName();
            String value = (String) declaredField.get(t);



            configManagementMapper.updateValue(name,value);



        }
    }
}
